﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using TLL.Finance.Currencys;
using TLL.Log;

namespace TLL.UTL
{
    public static class Money
    {
        public static Dictionary<CurrencyType, double> Calculate(LogTime time, double cashPerH, List<Currency> currencys, CurrencyType currency)
        {
            var res = new Dictionary<CurrencyType, double>();

            var rusUSDSale = currencys.FirstOrDefault(x => x.Country == CurrencyCountry.Russian && x.Type == CurrencyType.USD).Sale;
            var rusUSDBuy = currencys.FirstOrDefault(x => x.Country == CurrencyCountry.Russian && x.Type == CurrencyType.USD).Buy;
            var ukUSDBuy = currencys.FirstOrDefault(x => x.Country == CurrencyCountry.Ukraine && x.Type == CurrencyType.USD).AverageBuyRate;

            switch (currency)
            {
                case CurrencyType.RUB:
                    var rubCount = (time.Hour + time.Minutes / 60.0 + time.Second / 3600.0) * cashPerH;
                    res.Add(currency, rubCount);
                    res.Add(CurrencyType.USD, rubCount / rusUSDSale);
                    res.Add(CurrencyType.UAH, (rubCount / rusUSDSale) * ukUSDBuy);
                    break;
            }
            return res;
        }
        /// <summary>
        /// Return currency rate in your country
        /// </summary>
        /// <param name="Currency"></param>
        /// Current currency
        /// <param name="CashValues"></param>
        /// Currency values in your country
        /// <returns></returns>
        public static List<Currency> GetExchangeRate(CurrencyType Currency, List<Currency> Currencys)
        {
            var res = new List<Currency>();

            var RUB = Currencys.FirstOrDefault(x => x.Type == CurrencyType.RUB).Buy;
            var USD = Currencys.FirstOrDefault(x => x.Type == CurrencyType.USD).Buy;

            switch (Currency)
            {
                case CurrencyType.UAH:
                    res.Add(new Currency(Currency, 1, 0));
                    res.Add(new Currency(CurrencyType.RUB, 1 / RUB, 0));
                    res.Add(new Currency(CurrencyType.USD, 1 / USD, 0));
                    break;
                case CurrencyType.RUB:
                    res.Add(new Currency(Currency, 1, 0));
                    res.Add(new Currency(CurrencyType.UAH, Currencys.FirstOrDefault(x => x.Country == CurrencyCountry.Ukraine && x.Type == CurrencyType.RUB).AverageBuyRate, Currencys.FirstOrDefault(x => x.Country == CurrencyCountry.Ukraine && x.Type == CurrencyType.RUB).AverageSaleRate));
                    res.Add(new Currency(CurrencyType.USD, Currencys.FirstOrDefault(x => x.Country == CurrencyCountry.Russian && x.Type == CurrencyType.USD).AverageBuyRate, Currencys.FirstOrDefault(x => x.Country == CurrencyCountry.Russian && x.Type == CurrencyType.USD).AverageSaleRate));
                    break;
                case CurrencyType.USD:
                    res.Add(new Currency(Currency, 1, 0));
                    res.Add(new Currency(CurrencyType.RUB, USD / RUB, 0));
                    res.Add(new Currency(CurrencyType.UAH, USD, 0));
                    break;
            }
            return res;
        }
        public static string Formated(Dictionary<CurrencyType, double> Items)
        {
            var res = string.Empty;
            foreach (var item in Items)
                res += item.Key.ToString() + ": " + item.Value.ToString("0.00") + "    ";
            return res;
        }
    }
}
